//
//  PlayerInterfaceSprite.m
//  CandyCrushClone
//
//  Created by Petar on 4/3/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "PlayerInterfaceSprite.h"

static const int NAME_Z_POSITION = 1;
static const int INFO_Z_POSITION = 0;

@interface PlayerInterfaceSprite ()

@property (nonatomic) SKSpriteNode *playerInfoSprite;
@property (nonatomic) SKLabelNode *ratingLabel;
@property (nonatomic) SKLabelNode *gamesLabel;
@property (nonatomic) SKLabelNode *winsLabel;
@property (nonatomic) SKLabelNode *producesLabel;
@property (nonatomic) SKLabelNode *attackLabel;

@end

@implementation PlayerInterfaceSprite
{
    BOOL _isShowingPlayerInfo;
}

- (instancetype)initForLocalPlayer:(BOOL)isForLocalPlayer
{
    
    NSString *layerImageName = isForLocalPlayer ? @"localPlayerLayer" : @"otherPlayerLayer";
    
    if ( self = [super initWithImageNamed:layerImageName] ) {
        self.name = layerImageName;
        
        [self addTurnIndicator];
        
        // add nameLabel
        _nameLabel = [self labelWithFontSize:18 atPosition:CGPointMake(0, self.size.height/2 - 28) inNode:self];
        _nameLabel.zPosition = NAME_Z_POSITION;
        
        // add Cookie counters and Sprites
        _cookieType1CounterLabel = [self labelWithFontSize:20 atPosition:CGPointMake(-25, 33) inNode:self];
        _cookieType1Sprite = [self spriteWithImageName:@"targetCookieType1" atPosition:CGPointMake(-56, 42)];

        _cookieType2CounterLabel = [self labelWithFontSize:20 atPosition:CGPointMake(39, 33) inNode:self];
        _cookieType2Sprite = [self spriteWithImageName:@"targetCookieType2" atPosition:CGPointMake(8, 41)];
        
        _cookieType3CounterLabel = [self labelWithFontSize:20 atPosition:CGPointMake(-25, 2) inNode:self];
        _cookieType3Sprite = [self spriteWithImageName:@"targetCookieType3" atPosition:CGPointMake(-56, 10)];
        
        _cookieType4CounterLabel = [self labelWithFontSize:20 atPosition:CGPointMake(39, 2) inNode:self];
        _cookieType4Sprite = [self spriteWithImageName:@"targetCookieType4" atPosition:CGPointMake(7, 10)];
        
        CGPoint cookieType5CounterLabelPosition = isForLocalPlayer ? CGPointMake(39.5, -39) : CGPointMake(-24.5, -39);
        CGPoint cookieType5SpritePosition = isForLocalPlayer ? CGPointMake(7, -32) : CGPointMake(-56, -32);
        _cookieType5CounterLabel = [self labelWithFontSize:15 atPosition:cookieType5CounterLabelPosition inNode:self];
        _cookieType5Sprite = [self spriteWithImageName:@"targetCookieType5" atPosition:cookieType5SpritePosition];
        
        CGPoint cookieType6CounterLabelPosition = isForLocalPlayer ? CGPointMake(39.5, -69.5) : CGPointMake(-24.5, -69.5);
        CGPoint cookieType6SpritePosition = isForLocalPlayer ? CGPointMake(7, -64) : CGPointMake(-56, -64);
        _cookieType6CounterLabel = [self labelWithFontSize:15 atPosition:cookieType6CounterLabelPosition inNode:self];
        _cookieType6Sprite = [self spriteWithImageName:@"targetCookieType6" atPosition:cookieType6SpritePosition];
        
        CGPoint heroFrameSpritePosition = isForLocalPlayer ? CGPointMake(-35,-53) : CGPointMake(33,-53);
        NSString *heroFrameSpriteImageName = isForLocalPlayer ? @"localPlayerHeroFrame" : @"otherPlayerHeroFrame";
        _heroFrameSprite = [self spriteWithImageName:heroFrameSpriteImageName atPosition:heroFrameSpritePosition];
        _heroFrameSprite.zPosition = NAME_Z_POSITION;
        
        [self initPlayerInfoSpriteForLocalPlayer:isForLocalPlayer];
        
    }
    
    return self;
    
}


- (void)setHeroImageWithType:(NSInteger)heroType forLocalPlayer:(BOOL)isForLocalPlayer
{
    
    NSString *heroImageName = [NSString stringWithFormat:@"animal_%zd",heroType];
    _heroSprite = [SKSpriteNode spriteNodeWithImageNamed:heroImageName];
    [_heroSprite setScale:0.5f];
    [_heroFrameSprite addChild:_heroSprite];
    
    if ( isForLocalPlayer == NO ) {
        
        // Swap sprite
        _heroSprite.xScale = -0.5;
        _heroSprite.yScale = 0.5;
    }
    
}


- (SKSpriteNode *)spriteWithImageName:(NSString *)imageName atPosition:(CGPoint)position
{
    
    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:imageName];
    sprite.position = position;
    
    [self addChild:sprite];
    
    return sprite;

}


- (void)addTurnIndicator
{
    
    _turnIndicatorSprite = [SKSpriteNode spriteNodeWithImageNamed:@"turnIndicator"];
    _turnIndicatorSprite.zPosition = -1;
    [_turnIndicatorSprite setAlpha:0];
    [self addChild:_turnIndicatorSprite];
    
}


#pragma mark - Player Info

- (void)initPlayerInfoSpriteForLocalPlayer:(BOOL)isForLocalPlayer
{
    
    NSString *infoImageName = isForLocalPlayer ? @"localPlayerInfo" : @"otherPlayerInfo";
    
    _playerInfoSprite = [SKSpriteNode spriteNodeWithImageNamed:infoImageName];
    _playerInfoSprite.zPosition = INFO_Z_POSITION;
    [_playerInfoSprite setAlpha:0];
    
    [self addChild:_playerInfoSprite];
    
    // add labels
    _ratingLabel = [self labelWithFontSize:14 atPosition:CGPointMake(-1, 23)  inNode:_playerInfoSprite];
    _gamesLabel = [self labelWithFontSize:14 atPosition:CGPointMake(38, 1)  inNode:_playerInfoSprite];
    _winsLabel = [self labelWithFontSize:12 atPosition:CGPointMake(-41, 2)  inNode:_playerInfoSprite];
    
    CGPoint producesLabelPosition = isForLocalPlayer ? CGPointMake(27.5, -45.5) : CGPointMake(-33, -45.5);
    _producesLabel = [self labelWithFontSize:18 atPosition:producesLabelPosition inNode:_playerInfoSprite];
    
    CGPoint attackLabelPosition = isForLocalPlayer ? CGPointMake(27.5, -83) : CGPointMake(-33, -83);
    _attackLabel = [self labelWithFontSize:18 atPosition:attackLabelPosition inNode:_playerInfoSprite];
    
    _isShowingPlayerInfo = NO;
    
}


- (void)setPlayerInfoForPlayer:(Player *)player
{
    
    NSString *ratingText = [NSString stringWithFormat:@"%zd",player.rank];
    _ratingLabel.text = ratingText;
    
    NSInteger playedGames = player.wins + player.losses;
    NSString *playedGamesText = [NSString stringWithFormat:@"%zd",playedGames];
    _gamesLabel.text = playedGamesText;
    
    CGFloat winsPercentage = ((CGFloat)player.wins / (CGFloat)playedGames) * 100.0f;
    
    if ( isnan(winsPercentage) ) {
        winsPercentage = 0;
    }
    
    NSString *winsText = [NSString stringWithFormat:@"%.1f%%",winsPercentage];
    _winsLabel.text = winsText;
    
    _producesLabel.text = [NSString stringWithFormat:@"%zd",player.hero.produces];
    _attackLabel.text = [NSString stringWithFormat:@"%zd", ABS(player.hero.attack)];
    
}


- (void)changeView
{
    
    if ( _isShowingPlayerInfo == NO ) {
        
        _isShowingPlayerInfo = YES;
        
        // Show player info
        [_playerInfoSprite runAction:[SKAction fadeInWithDuration:0.5]];
        
    } else {
        
        _isShowingPlayerInfo = NO;
        
        // Hide player info
        [_playerInfoSprite runAction:[SKAction fadeOutWithDuration:0.5]];
        
    }
    
}


#pragma mark - Helper Methods
- (SKLabelNode *)labelWithFontSize:(NSInteger)fontSize atPosition:(CGPoint)position inNode:(SKNode *)parent
{
    
    SKLabelNode *label = [SKLabelNode labelNodeWithFontNamed:@"GillSans-BoldItalic"];
    label.fontSize = fontSize;
    label.text = [NSString stringWithFormat:@"20"];
    label.position = position;
    
    [parent addChild:label];
    
    return label;
    
}


@end
